using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using microsoft_TODO.DTOs;
using microsoft_TODO.MongoCore;
using microsoft_TODO.ServiceCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using server.Helpers;

namespace microsoft_TODO.Controllers {

  [Route ("api/[controller]")]
  public class ListController : Controller {

    ILogger<ListController> _logger;
    IListService _listService;

    public ListController (ILogger<ListController> logger,
      IListService listService,
      IMapper mapper) {
      _logger = logger;
      _listService = listService;
    }

    /// <summary>
    /// 获取所有清单列表
    /// </summary>
    /// <param name="uid"></param>
    /// <returns></returns>
    [HttpGet ("{uid}")]
    public IActionResult Get (string uid) {
      try {
        var res = _listService.GetAllByUserId (uid);
        _logger.LogInformation (LoggingEvents.GetItems, "Lists({COUNT}) Getted", res.Count);
        return Ok (res);
      } catch (Exception exp) {
        _logger.LogError ("Failed to execute GET: {mssage}", exp.Message);
        return BadRequest ();
      }
    }

    /// <summary>
    /// 获取清单名称
    /// </summary>
    /// <param name="lid">清单ID</param>
    /// <returns></returns>
    [HttpGet ("{lid}/title")]
    public IActionResult GetTitle (string lid) {
      try {
        var res = _listService.GetTitleByListId (lid);
        return Ok (res);
      } catch (Exception exp) {
        _logger.LogError ("Failed to execute GETTITLE: {message}", exp.Message);
        return BadRequest ();
      }
    }

    /// <summary>
    /// 新建清单
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPost]
    public IActionResult Post ([FromBody] NewListDto dto) {
      try {
        var list = _listService.InsertNewList (dto);
        _logger.LogInformation (LoggingEvents.InsertItem, "List({ID}) Inserted", list.Id);
        return Created ("", list);
      } catch (Exception exp) {
        _logger.LogError ("Failed to execute POST: {message}", exp.Message);
        return BadRequest ();
      }
    }

    /// <summary>
    /// 更新清单名
    /// </summary>
    /// <param name="dto"></param>
    /// <returns></returns>
    [HttpPut ("title")]
    public IActionResult RenameTitle ([FromBody] ListTitleDto dto) {
      try {
        _listService.ModifyTitle (dto);
        _logger.LogInformation (LoggingEvents.UpdateItem, "List({ID}) Title Updated", dto.Id);
        return Ok ();
      } catch (Exception exp) {
        _logger.LogError ("Failed to execute PUT: {message}", exp.Message);
        return BadRequest ();
      }
    }

    /// <summary>
    /// 删除指定清单
    /// </summary>
    /// <param name="id">List Id</param>
    /// <returns></returns>
    [HttpDelete ("{id}")]
    public IActionResult Delete (string id) {
      try {
        if (!_listService.IsExistent (id)) {
          _logger.LogWarning (LoggingEvents.DeleteItemNotFound, "List({ID}) Delete NOT FOUND", id);
          return NotFound (new { code = LoggingEvents.DeleteItemNotFound });
        }
        _listService.RemoveList (id);
        _logger.LogInformation (LoggingEvents.DeleteItem, "List({ID}) Deleted", id);
        return Ok ();
      } catch (Exception exp) {
        _logger.LogError ("Failed to execute DELETE list({ID}): {message}", id, exp.Message);
        return BadRequest ();
      }
    }
  }
}